import machine
import ubinascii

# 设备ID
CLIENT_ID = ubinascii.hexlify(machine.unique_id()).decode("utf-8")
# MQTT 主题路径
TOPIC = "device/led/{}".format(CLIENT_ID)
TOPIC = TOPIC.encode()
# 点亮LED
pin_12 = machine.Pin(12, machine.Pin.OUT)
pin_12.off()


def register_led_Topic(client):
    client.subscribe(TOPIC)


def handleLed(topic, msg):
    if topic != TOPIC:  # 确保消息来自正确的主题
        return
    try:
        led_status = msg.decode()
        if led_status == "on":
            pin_12.on()
        else:
            pin_12.off()
    except ValueError as e:
        # 如果消息不是有效的JSON，捕获异常
        print("Error parsing JSON:", e)
